import pygame

class Button(object):

	def __init__(self, x, y, gui, start_image, finish_image, func = None, stay_depressed = False):
		'''a button that calls a function when pressed'''
		self.x = x
		self.y = y
		self.start_image = pygame.image.load(start_image)
		self.finish_image = pygame.image.load(finish_image)
		self.stay_depressed = stay_depressed
		self.func = func
		self.height = self.start_image.get_height()
		self.width = self.start_image.get_width()
		self.gui = gui
		self.gui.clicked[self] = False
		self.gui.buttons.append(self)
		self.draw_button()
		
	def click_button(self, x, y):
		'''handles a button's click'''
		if x >= self.x and x <= self.x + self.width:
			if y >= self.y and y <= self.y + self.height:
				self.gui.clicked[self] = not self.gui.clicked[self]
				if self.gui.clicked:
					self.func()
				self.draw_button()
		
	def unclick_button(self):
		'''handles a button that automatically comes up'''
		if not self.stay_depressed:
			self.gui.clicked[self] = False
			if not self.gui.clicked:
				self.func()
			self.draw_button()
			return True
	
	def draw_button(self):
		'''draws an image of a button'''
		if not self.gui.clicked[self]:
			surface = self.start_image
		else:
			surface = self.finish_image
		self.gui.window.blit(surface, (self.x,self.y))


		
	

